package com.yanqu.road.dao.impl.palace;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.estatewar.palace.PalaceUserData;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PalaceUserDataDaoImpl extends TempDataObjectDao<PalaceUserData> {

    public void getPalaceUserDataMap(int activityId, Map<Long, List<PalaceUserData>> palaceAideMap, Map<Long, PalaceUserData> palaceUserDataMap) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_c_palace_user_data where activityId=?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                PalaceUserData data = parse(rs);
                if(!palaceAideMap.containsKey(data.getAideUserId())){
                    palaceAideMap.put(data.getAideUserId(), new ArrayList<>());
                }
                palaceAideMap.get(data.getAideUserId()).add(data);
                palaceUserDataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
    }

    @Override
    protected boolean addData(PalaceUserData data) {
        PreparedStatement call = null;
        String sql = "insert into t_c_palace_user_data(activityId,userId,aideUserId,joinTime,onlyUnionMemberJoin)" +
                " values (?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getAideUserId());
            call.setLong(i++, data.getJoinTime());
            call.setBoolean(i++, data.isOnlyUnionMemberJoin());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(PalaceUserData data) {
        PreparedStatement call = null;
        String sql = "update t_c_palace_user_data set aideUserId=?,joinTime=?,onlyUnionMemberJoin=?" +
                " where activityId=? and userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getAideUserId());
            call.setLong(i++, data.getJoinTime());
            call.setBoolean(i++, data.isOnlyUnionMemberJoin());
            call.setInt(i++, data.getActivityId());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected PalaceUserData parseData(ResultSet rs) throws SQLException {
        PalaceUserData data = new PalaceUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setAideUserId(rs.getLong("aideUserId"));
        data.setJoinTime(rs.getLong("joinTime"));
        data.setOnlyUnionMemberJoin(rs.getBoolean("onlyUnionMemberJoin"));
        return data;
    }
}
